﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPOI.HSSF.UserModel;
using System.IO;
using HNQXYJ.Model;
using HNQXYJ.BLL;
namespace HNQXYJ.HN_DATA
{
    public partial class DataIO : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string filePath = this.fileName.Value;
            if (filePath != "")
            {
                
                using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook workbook = new HSSFWorkbook(stream);
                    HSSFSheet sheet =(HSSFSheet) workbook.GetSheetAt(0);
                    Terminal tm = new Terminal();
                    for (int i =1; i <= sheet.LastRowNum; i++)
                    {
                        HSSFRow row = (HSSFRow)sheet.GetRow(i);
                        tm.Phone = row.GetCell(0).NumericCellValue.ToString();
                        tm.TownName = row.GetCell(1).StringCellValue;
                        tm.AreaCode = row.GetCell(2).StringCellValue;
                        tm.Leader = row.GetCell(3).StringCellValue;
                        tm.LeaderPhone = row.GetCell(4).NumericCellValue.ToString();
                        tm.Statuss = row.GetCell(5).StringCellValue;
                        tm.Lng =Convert.ToDouble( row.GetCell(6).NumericCellValue);
                        tm.Lat =Convert.ToDouble( row.GetCell(7).NumericCellValue);
                        tm.RankLevel = row.GetCell(8).StringCellValue;
                        tm.Category = row.GetCell(9).NumericCellValue.ToString();
                        tm.AddDate = DateTime.Now.ToShortDateString();
                        tm.Notes = "无";
                        int  res = TerminalBLL.InsertTerminalData(tm);
                    }
                    this.Label1.Text = "数据导入完成！";

                }

            }
            else
            {
                this.Label1.Text = "请选择文件";
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string filePath = this.fileCounty.Value;
            if (filePath != "")
            {
                using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook workbook = new HSSFWorkbook(stream);
                    HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);
                    HCounty county = new HCounty();
                    for (int i = 9; i < sheet.LastRowNum; i++)
                    {
                        HSSFRow row =(HSSFRow) sheet.GetRow(i);
                        county.Name = row.GetCell(0).StringCellValue;
                        county.ParentCode = row.GetCell(1).StringCellValue;
                        county.SelfCode = row.GetCell(2).StringCellValue;
                        county.DateAdd = DateTime.Now;
                        county.Lng = row.GetCell(3).NumericCellValue;
                        county.Lat = row.GetCell(4).NumericCellValue;

                        int res = new CountyBLL().AddCounty(county);
                    }
                    this.Label2.Text = "数据导入完成！";
                }
            }
        }
    }
}